
#include "Context/Gameplay/Car.h"
#include "Context/Gameplay/Driver.h"
#include "Context/Gameplay/Track.h"

namespace Context {
std::list<Car *> Car::runningCars;

Car::Car(std::string carName, Context::Track * track, Driver * driver)
	: Engine::Reason("Gameplay")
	, Engine::Sense("Gameplay")
	, Engine::Visual("Gameplay")
	, name(carName)
	, track(track)
	, driver(driver)
	, carGfx(carName, "racer", 10) {
	/* We should load a new driver and attach it to the car */
	driver->getInto(this);
	runningCars.push_back(this);

	/* TODO: regulate these values */
	movement.setAccel(500.0f);
	Misc::Vector2d v(1.0f, 0.0f);
	movement.setVelocity(v);
	movement.setFriction(200.0f);
	movement.setMaxSpeed(1000.0f);
}

void Car::pulse(int delta) {
	double angle = orientation;
	if (reversing) movement.setAccel(-500.0f);
	else movement.setAccel(500.0f);
	position = movement.update(delta, throttling || reversing);
	carGfx.setPosition(position.x, position.y);
	carGfx.setRotation(angle);
	
	/* throttling gets updated if throttle() gets called */
	throttling = false;
	reversing = false;
}

Engine::Resource::GraphicList Car::display() {
	Engine::Resource::GraphicList gfxList;
	gfxList.push_back(&carGfx);
	return gfxList;
}

Car::~Car() {
}


} // namespace Context
